<?php namespace App\Http\Controllers\Api;

use App\Exceptions\NotFoundException;
use App\Exceptions\SecurityException;
use App\Goods;
use App\OrderAddress;
use App\OrderItem;
use App\Orders;
use App\Services\Tools;
use App\User;
use App\UserAddress;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

class OrderController extends ApiController
{
    public function buildOrderItem(){
        $goodId = Input::get('good_id');
        $num    = Input::get('num');

        /** @var Goods $thatGood */
        $thatGood = Goods::whereId($goodId)->whereIsshop(2)->first();

        if(!$thatGood)
            throw new NotFoundException(NotFoundException::CommodityNotFound);

        $thatOrderItem = new OrderItem();
        $thatOrderItem->good_id = $thatGood->id;
        $thatOrderItem->order_num = Tools::makeOrderNum();
        $thatOrderItem->m_price   = (float)$thatGood->m_price * (float)$num;
        $thatOrderItem->s_price   = (float)$thatGood->s_price * (float)$num;
        $thatOrderItem->number    = $num;

        $thatOrderItem->save();

        Session::put('orderitemid', $thatOrderItem->id);

        return $this->buildResponse(trans('子订单生成成功'),$thatOrderItem);
    }

    public function changeOrderItemNum(){
        $orderItemId = Input::get('id');
        $num         = Input::get('num');

        /** @var OrderItem $thatOrderItem */
        $thatOrderItem = OrderItem::find($orderItemId);

        if(!$thatOrderItem)
            throw new NotFoundException(NotFoundException::OrderNotFound);

        $thatGood = $thatOrderItem->good;

        $thatOrderItem->number  = $num;
        $thatOrderItem->s_price = (float)$thatGood->s_price * (float)$num;
        $thatOrderItem->m_price = (float)$thatGood->m_price * (float)$num;
        $thatOrderItem->save();

        return $this->buildResponse(trans('修改子订单数量成功'),$thatOrderItem);
    }

    public function confirmOrder(Guard $guard){
        if($guard->guest())
            throw new SecurityException(SecurityException::LoginFirst);

        $orderItemId = Input::get('id');
        $addrId      = Input::get('addr_id');
        $note        = Input::get('note');

        /** @var User $user */
        $user = $guard->user();

        /*启用事物*/
        $result = DB::transaction(function () use ($orderItemId,$addrId,$note,$user){
            /** @var OrderItem $thatOrderItem */
            $thatOrderItem = OrderItem::find($orderItemId);

            if (!$thatOrderItem)
                throw new NotFoundException(NotFoundException::OrderNotFound);

            $thatGood = $thatOrderItem->good;
            if(!$thatGood)
                throw new NotFoundException(NotFoundException::CommodityNotFound);

            $exp_fee = 0;
            if ((int)$thatOrderItem->number < 5 || (int)$thatOrderItem->number > 10){
                $exp_fee = $thatGood->express_fee;
            }

            /** @var UserAddress $thatUserAddress */
            $thatUserAddress = UserAddress::find($addrId);
            if (!$thatUserAddress)
                throw new NotFoundException(NotFoundException::AddressNotFound);

            $orderAddressModel              = new OrderAddress();
            $orderAddressModel->user_id     = $user->id;
            $orderAddressModel->consignee   = $thatUserAddress->consignee;
            $orderAddressModel->province    = $thatUserAddress->province;
            $orderAddressModel->city        = $thatUserAddress->city;
            $orderAddressModel->area        = $thatUserAddress->area;
            $orderAddressModel->add_detail  = $thatUserAddress->add_detail;
            $orderAddressModel->mobilephone = $thatUserAddress->mobilephone;
            $orderAddressModel->save();

            $orderModel              = new Orders();
            $orderModel->user_id     = $user->id;
            $orderModel->order_num   = Tools::makeOrderNum();
            $orderModel->m_amount    = $thatOrderItem->m_price;
            $orderModel->s_amount    = $thatOrderItem->s_price;
            $orderModel->express_fee = $exp_fee;
            $orderModel->address_id  = $orderAddressModel->id;
            $orderModel->note        = $note;
            $orderModel->save();

            $thatOrderItem->order_id = $orderModel->id;
            $thatOrderItem->save();

            $thatGood->stock = (int)$thatGood->stock - (int)$thatOrderItem->number;
            $thatGood->save();

            return $orderModel;
        });

        return $this->buildResponse(trans('下单成功'),$result);
    }
}